<?php
/**
 * User:于小涛
 * Date:2020/2/17
 * Time:15:37
 */
namespace app\admin\controller\rbac;

use app\admin\BaseController;
use app\admin\model\rbac\Role as RoleModel;
use app\admin\business\rbac\Role as RoleBusiness;
use think\App;
use think\facade\View;

class Role extends BaseController {

    private $obj_role_model=null;
    private $obj_role_business=null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_role_model = new RoleModel();
        $this->obj_role_business = new RoleBusiness();
    }

    public function getList(){
        return view();
    }

    public function getListData(){
        $arr_data=$this->obj_role_business->getAuthListStruct();
        return output_data($arr_data);
    }

    public function add(){
        if($this->request->param('name')){
            $arr_data_add=array(
                'name'=>$this->request->param('name'),
                'cpid'=>$this->request->param('cpid'),
                'cid'=>$this->obj_role_business->getCidByCpid($this->request->param('cpid')),
            );
            $int_id=$this->obj_role_model->add($arr_data_add);
            if($int_id){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            $str_option_html=$this->obj_role_business->getRoleHtmlOption('0');
            View::assign('str_option_html',$str_option_html);
            return view();
        }
    }

    public function edit(){
        $arr_info=$this->obj_role_model->findOrEmpty($this->request->param('id'))->toArray();
        if($this->request->param('name')){
            $arr_data_edit=array(
                'name'=>$this->request->param('name'),
            );
            //暂时不允许修改结构，这个改起来有点麻烦，意义也不大
//            if(strpos($arr_info['cid'],$this->request->param('cpid'))!==0&&$this->request->param('cpid')!=0){
//                $arr_data_edit['cpid'] = $this->request->param('cpid');
//                $arr_data_edit['cid'] = $this->obj_role_business->getCidByCpid($this->request->param('cpid'));
//            }
            $int_num=$this->obj_role_model->edit($this->request->param('id'),$arr_data_edit);
            if($int_num){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            View::assign('arr_data_info',$arr_info);
            $str_option_html=$this->obj_role_business->getRoleHtmlOption('0',substr($arr_info['cid'],0,-3),array($this->request->param('id')));
            View::assign('str_option_html',$str_option_html);
            return view();
        }
    }

    public function del(){
        $arr_data_edit=array(
            'is_del'=>2,
        );
        $int_num=$this->obj_role_model->edit($this->request->param('id'),$arr_data_edit);
        if($int_num){
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }

    public function authConfig(){
        if($this->request->param('role_id')){
            $this->obj_role_business->configAuth($this->request->param('role_id'),array_unique($this->request->param('authids')));
            return output_data();
        }else{
            return view();
        }
    }

    public function roleTree(){
        $arr_list_data=$this->obj_role_business->getRoleTree($this->request->param('admin_id'));
        return output_data($arr_list_data);
    }


}